//
//  LBTagCollectionCell.m
//  app
//
//  Created by 刘文扬 on 16/11/30.
//  Copyright © 2016年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBTagCollectionCell.h"
#import "LBTagsModelProtocol.h"
#import "Masonry.h"
static CGFloat kTopSpace = 2;

@interface LBTagCollectionCell()

/** lable*/
@property(nonatomic,strong)UILabel *contentLabel;

/** imageView*/
@property(nonatomic,strong)UIImageView *lb_backgroundImageView;

/** gapLineView*/
@property(nonatomic,strong)UIView *gapLineView;

@end

@implementation LBTagCollectionCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initSubViews];
    }
    return self;
}

-(void)initSubViews
{
    [self addSubview:self.lb_backgroundImageView];
    [self.lb_backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
    
    
    
    [self addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.and.centerY.equalTo(self);
        
    }];
    
    [self addSubview:self.gapLineView];
    [self.gapLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.height.equalTo(@10);
        make.trailing.offset(0);
    }];
    
}


+(CGSize)sizeForItem:(id)data indexPath:(NSIndexPath *)indexPath totalListNumber:(NSInteger)totalListNumber leftAndRightMargin:(NSInteger)leftAndRightMargin textFont:(UIFont *)textFont itemHeight:(CGFloat)itemHeight
{
    NSString *content;
    if ([data isKindOfClass:[NSString class]]) {
        content = data;
    }else{
        id<LBTagsModelProtocol> model = data;
        content  = [model nameToshow];
    }
    
    CGSize contentSize = [LBTagCollectionCell getTextSizeWithContent:content Font:textFont Width:[[UIScreen mainScreen] bounds].size.width];
    if (itemHeight != 0) {
        return CGSizeMake(contentSize.width + leftAndRightMargin * 2, itemHeight);
    }
    return CGSizeMake(contentSize.width + leftAndRightMargin * 2, contentSize.height + kTopSpace * 2);
}

-(void)configCellWithData:(id)data atIndexPath:(NSIndexPath *)indexPath totalListNumber:(NSInteger)totalListNumber
{
    NSString *content;
    if ([data isKindOfClass:[NSString class]]) {
        content = data;
    }else{
        id<LBTagsModelProtocol> model = data;
        content  = [model nameToshow];
    }
    
    self.gapLineView.hidden = !self.haveGapLine;
    self.contentLabel.text = content;
    self.contentLabel.textColor = self.textColor;
    self.contentLabel.font = self.textFont;
    self.backgroundColor = self.itemBackgroundColor;
    
    if (self.lb_backgroundImage != nil) {
        UIImage *image = [self.lb_backgroundImage resizableImageWithCapInsets:self.imageEdgeInsets];
        self.lb_backgroundImageView.image = image;
    }
    
    if ([data respondsToSelector:@selector(selectedPropertyName)]) {
        NSNumber *isSelect = [data valueForKey:[data selectedPropertyName]];
        if (isSelect.boolValue) {
            if (self.lb_selectedBackgroundImage) {
                UIImage *selectedImage = [self.lb_selectedBackgroundImage resizableImageWithCapInsets:self.imageEdgeInsets];
                self.lb_backgroundImageView.image = selectedImage;
                self.contentLabel.textColor = self.selectTextColor;
            }
        }
    }
    
    
}

-(UILabel *)contentLabel
{
    if (_contentLabel == nil) {
        
        UILabel *label = [[UILabel alloc] init];
        label.font = self.textFont;
        label.textColor = self.textColor;
        label.numberOfLines = 1;
        _contentLabel = label;
        
    }
    return _contentLabel;
}

+ (CGSize)getTextSizeWithContent:(NSString *)content
                            Font:(UIFont *)font
                           Width:(CGFloat)width
{
    UILabel *tmpLable = [UILabel new];
    [tmpLable setText:nil];
    [tmpLable setAttributedText:nil];
    tmpLable.frame = CGRectMake(0, 0, width, CGFLOAT_MAX);
    tmpLable.numberOfLines = 0;
    tmpLable.lineBreakMode = NSLineBreakByWordWrapping;
    tmpLable.textAlignment = NSTextAlignmentJustified;
    tmpLable.font = font;
    [tmpLable setText:content];
    [tmpLable sizeToFit];
    
    CGSize realSize = tmpLable.frame.size;
    
    if (realSize.width > width) {
        realSize.width = width;
    }
    
    return realSize;
}


-(UIImageView *)lb_backgroundImageView
{
    if (_lb_backgroundImageView == nil) {
        
        _lb_backgroundImageView = [[UIImageView alloc] init];
        
    }
    return _lb_backgroundImageView;
}

-(UIView *)gapLineView
{
    if (_gapLineView == nil) {
        
        _gapLineView = [[UIView alloc] init];
        _gapLineView.backgroundColor = [UIColor colorWithRed:155.0/255.0 green:155.0/255.0 blue:155.0/255.0 alpha:1];
    }
    return _gapLineView;
}



@end
